use crate::lib_namespace::namespace;
use crate::lib_namespace_d::namespace_d;
use libc::{c_char, c_int, exit, printf};
use std::env::args;
use crate::lib_namespace_exec::lib_namespace_exec;

mod lib_cgroup;
mod lib_init_web;
mod lib_namespace;
mod lib_namespace_d;
mod lib_ns_ctrl_c;
mod lib_ns_rootfs;
mod lib_ns_ugid_map;
mod lib_namespace_exec;

fn main() {
    unsafe {
        if args().len() == 1 {
            print_banner();
            exit(1);
        }
        let count = args().count();
        for i in  1..count  {
            let arg = args().nth(i).expect("no arg");
            let arg_trim = arg.trim();
            if arg_trim == "-h" || arg_trim == "--help" {
                print_banner();
            }
            if arg_trim == "-d" {
                let execute_able = args().nth(i+1).expect("must specify the -d argument");
                let execute_able = execute_able.trim().as_ptr() as *const c_char;
                namespace_d(execute_able);
                return;
            }
            if arg_trim == "-e" || arg_trim == "--exec" {
                let pid = args().nth(i + 1).expect("must specify the exec argument");
                let execute_able = args().nth(i + 2).expect("must specify the exec argument");
                let pid = pid.trim().as_ptr() as *const c_char;
                let execute_able = execute_able.trim().as_ptr() as *const c_char;
                lib_namespace_exec(pid,execute_able);
                return;
            }
        }

        let execute_able = args().last().unwrap();
        let execute_able = execute_able.trim().as_ptr() as *const c_char;
        namespace(execute_able);
        return;
    }
}
fn print_banner() {
    println!("Usage: ./untitled [options] <exe>");
    println!("Options:");
    println!("  -h, --help                Show this help message");
    println!("  -d                        Run container backend");
    println!("  -e  --exec <pid> <exe>    Execute cmd in container")
}
